package com.daohuoren.algorithm;

class insertion_sort {
	public static void insertion(int[] arr)
	 {   
	     int i,j,t;
	 	for (i = 1; i < arr.length; i++ ){
	 		t = arr[i];
	 		j = i - 1; 
	 		while(j >= 0 && arr[j] > t){
	 			arr[j + 1] = arr[j];
	 			j--;
			}
	 		arr[j+1] = t;
	 		printArray(arr);
		}
	}
	public static void main (String[] args) throws java.lang.Exception
	{
		 int[] arr = {6,5,3,1,8,7,2,4};
		 printArray(arr);
		 insertion(arr);
	}
	public static void printArray(int[] arr){
	    for(int i : arr){
	        System.out.print(i+"   ");
	    }
	    System.out.println();
	}
}
